#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QIODevice>
#include <QThread>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    _port = new QextSerialPort();

    connect(_port, &QIODevice::readyRead, [=]()
    {
        qDebug() << _port->readAll();
    });

    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked() {

    _port->setPortName("COM13");

    _port->setBaudRate(BaudRateType::BAUD921600);
    _port->setDataBits(DataBitsType::DATA_8);
    _port->setParity(ParityType::PAR_NONE);
    _port->setStopBits(StopBitsType::STOP_1);
    _port->setFlowControl(FlowType::FLOW_OFF);
    _port->setTimeout(1000);
    // _port->setTimeout(1, 0);

    _port->open(QIODevice::ReadWrite);

    qDebug() << _port->isOpen();


}

void MainWindow::on_pushButton_2_clicked() {

    QString send = "68 01 68 01 01 7e 80 16";
    QByteArray sendArray;
    QStringList sendList = send.split(' ');
    for (auto one : sendList)
    {
        bool ok ;
        sendArray.append(one.toUInt(&ok, 16));
    }

    _port->write(sendArray);

    // QThread::msleep(100);



    // QByteArray buf = _port->readAll();
    // qDebug() << buf;


}
